package com.changpay.zywy.ui.activity.count

import android.app.DatePickerDialog
import android.view.View
import com.changpay.zywy.R
import com.changpay.zywy.base.BaseMvpActivity
import com.changpay.zywy.mvp.contract.CountTrendContract
import com.changpay.zywy.bean.TrendBean
import com.changpay.zywy.mvp.presenter.CountTrendPresenter
import com.changpay.zywy.utils.DateUtil
import com.changpay.zywy.utils.DialogUtil
import kotlinx.android.synthetic.main.activity_count_trend.*
import kotlinx.android.synthetic.main.title_layout.*
import java.text.DecimalFormat
import java.util.*

class CountTrendActivity : BaseMvpActivity<CountTrendContract.View, CountTrendContract.Presenter>(),
    CountTrendContract.View {

    private var start_date = ""
    private var end_date = ""
    val string_array_one: Array<String> = arrayOf("交易量", "商户交易量", "渠道交易量", "总交易量")
    val string_array_two: Array<String> = arrayOf("激活量", "商户激活量", "渠道激活量", "总激活量")
    val string_array_three: Array<String> = arrayOf("收益量", "商户收益量", "渠道收益量", "总收益量")

    private val ChartXs = ArrayList<String>()  // 图表X轴坐标
    private val ChartYs = ArrayList<String>()  // 图表Y轴坐标
    private val ChartDatePointsOne = ArrayList<String>()  // 图表点1
    private val ChartDatePointsTwo = ArrayList<String>()  // 图表点2
    private val ChartDatePointsThree = ArrayList<String>()  // 图表点3
    private val PrograssFirst = ArrayList<Int>()
    private val PrograssSecond = ArrayList<Int>()
    private val PrograssThird = ArrayList<Int>()
    private var max: Double = 0.toDouble()

    private lateinit var onDateSetListener: DatePickerDialog.OnDateSetListener

    private val onClickListener = View.OnClickListener { view ->
        when (view.id) {
            R.id.trend_tv_check_time -> onDateSetListener.let {
                DialogUtil.showDatePickerDialog(
                    this,
                    it,
                    Calendar.getInstance()
                )
            }
        }
    }

    private var type: Int = 0
    override fun attachLayoutRes(): Int = R.layout.activity_count_trend

    override fun showTrend(data: TrendBean) {
        initData(data)


    }


    override fun createPresenter(): CountTrendContract.Presenter = CountTrendPresenter()


    override fun initData() {

    }

    // 默认请求
    override fun start() {
        val today = DateUtil.getToday()
        initDateData(today)
    }


    override fun initView() {
        super.initView()
        tv_title.text = "趋势图"
        type = intent.getIntExtra("type", 1)
        when (type) {
            1 -> {
                count_trend_title.text = string_array_one[0]
                count_trend_title_one.text = string_array_one[1]
                count_trend_title_two.text = string_array_one[2]
                count_trend_title_three.text = string_array_one[3]
            }
            2 -> {
                count_trend_title.text = string_array_two[0]
                count_trend_title_one.text = string_array_two[1]
                count_trend_title_two.text = string_array_two[2]
                count_trend_title_three.text = string_array_two[3]
            }
            3 -> {
                count_trend_title.text = string_array_three[0]
                count_trend_title_one.text = string_array_three[1]
                count_trend_title_two.text = string_array_three[2]
                count_trend_title_three.text = string_array_three[3]
            }
        }
        iv_back.setOnClickListener { finish() }
        trend_tv_check_time.setOnClickListener(onClickListener)
        onDateSetListener = DatePickerDialog.OnDateSetListener { datePicker, year, month, day ->
            val select_date = year.toString() + "-" + (month + 1) + "-" + day

            initDateData(select_date)
        }
        trend_tv_check_time.setOnClickListener(onClickListener)
    }


    private fun initDateData(select_date: String) {
        val param = hashMapOf<String, String>()
        start_date = DateUtil.getSpecifiedDayBefore(select_date, 6)
        end_date = select_date
        trend_time.text = "$start_date 至 $end_date"
        param["key"] = token
        param["type"] = type.toString()
        param["search_date_star"] = start_date
        param["search_date_end"] = end_date
        param["gc_id"] = intent.getStringExtra("gc_id")

        mPresenter?.getTrend(param)

    }

    private fun initData(countLineBean: TrendBean) {
        ChartXs.clear()
        ChartYs.clear()
        ChartDatePointsOne.clear()
        ChartDatePointsTwo.clear()
        ChartDatePointsThree.clear()


        // 1.先算出最大的的线条
        ChartDatePointsOne.add(countLineBean.personal.personal_one)
        ChartDatePointsOne.add(countLineBean.personal.personal_two)
        ChartDatePointsOne.add(countLineBean.personal.personal_three)
        ChartDatePointsOne.add(countLineBean.personal.personal_four)
        ChartDatePointsOne.add(countLineBean.personal.personal_five)
        ChartDatePointsOne.add(countLineBean.personal.personal_six)
        ChartDatePointsOne.add(countLineBean.personal.personal_seven)

        ChartDatePointsTwo.add(countLineBean.group.group_one)
        ChartDatePointsTwo.add(countLineBean.group.group_two)
        ChartDatePointsTwo.add(countLineBean.group.group_three)
        ChartDatePointsTwo.add(countLineBean.group.group_four)
        ChartDatePointsTwo.add(countLineBean.group.group_five)
        ChartDatePointsTwo.add(countLineBean.group.group_six)
        ChartDatePointsTwo.add(countLineBean.group.group_seven)

        ChartDatePointsThree.add(
            (countLineBean.personal.personal_one.toDouble() +
                    countLineBean.group.group_one.toDouble()).toString()
        )
        ChartDatePointsThree.add(
            (countLineBean.personal.personal_two.toDouble() +
                    countLineBean.group.group_two.toDouble()).toString()
        )

        ChartDatePointsThree.add(
            (countLineBean.personal.personal_three.toDouble() +
                    countLineBean.group.group_three.toDouble()).toString()
        )

        ChartDatePointsThree.add(
            (countLineBean.personal.personal_four.toDouble() +
                    countLineBean.group.group_four.toDouble()).toString()
        )

        ChartDatePointsThree.add(
            (countLineBean.personal.personal_five.toDouble() +
                    countLineBean.group.group_five.toDouble()).toString()
        )

        ChartDatePointsThree.add(
            (countLineBean.personal.personal_six.toDouble() +
                    countLineBean.group.group_six.toDouble()).toString()
        )

        ChartDatePointsThree.add(
            (countLineBean.personal.personal_seven.toDouble() +
                    countLineBean.group.group_seven.toDouble()).toString()
        )


        // 构建x轴 .分别获取前6天
        ChartXs.add(DateUtil.getSpecifiedDayBeforeFomat(end_date, 6))
        ChartXs.add(DateUtil.getSpecifiedDayBeforeFomat(end_date, 5))
        ChartXs.add(DateUtil.getSpecifiedDayBeforeFomat(end_date, 4))
        ChartXs.add(DateUtil.getSpecifiedDayBeforeFomat(end_date, 3))
        ChartXs.add(DateUtil.getSpecifiedDayBeforeFomat(end_date, 2))
        ChartXs.add(DateUtil.getSpecifiedDayBeforeFomat(end_date, 1))
        ChartXs.add(DateUtil.getSpecifiedDayBeforeFomat(end_date, 0))

        // 构建Y轴
        if (ChartXs.isNotEmpty() && ChartDatePointsOne.isNotEmpty()) {
            val size = ChartXs.size
            // 求出最大值
            max = 0.0
            for (i in 0 until size) {
                val data_three = java.lang.Double.valueOf(ChartDatePointsThree[i])
                if (data_three > max) {
                    max = data_three
                }
            }


            // 选算出有几位
            if(max < 5) max = 5.0
            val maxInt = max.toInt()
            val lengthNum = LengthNum(maxInt)
            var pow: Int
            if (lengthNum == 0) {
                pow = 2
            } else {
                // 1.获取平均数
                pow = maxInt / 5
                // 2.算出平均数有多少位
                if (pow < 1) pow = 1
            }

            for (i in 0..5) {
                val values = pow * i
                if (lengthNum >= 5) {
                    ChartYs.add(formatDouble(values.toFloat()))
                } else {
                    ChartYs.add(values.toString())
                }
            }


            if (lengthNum >= 5) {
                if (lengthNum >= 9) {
                    tvUiit.text = "亿/单位"
                    tvUiit.visibility = View.VISIBLE
                } else {
                    tvUiit.text = "万/单位"
                    tvUiit.visibility = View.VISIBLE
                }
            } else {
                tvUiit.text = ""
                tvUiit.visibility = View.GONE
            }
        }


        setData()
    }


    fun LengthNum(num: Int): Int {
        var nums = num
        var count = 0 //计数
        while (nums >= 1) {
            nums /= 10
            count++
        }
        return count
    }

    private fun formatDouble(value: Float): String {
        val n = (value / 10000).toDouble()
        val decimalFormat = DecimalFormat("0.0")
        return decimalFormat.format(n)
    }


    private fun setData() {
        PrograssFirst.clear()
        PrograssSecond.clear()
        PrograssThird.clear()
        if (max == 0.0) {
            for (i in 0..6) {
                PrograssFirst.add(0)
                PrograssSecond.add(0)
                PrograssThird.add(0)
            }
        } else {
            for (i in 0..6) {
                val one = (java.lang.Double.valueOf(ChartDatePointsOne[i]) / max * 100).toInt()
                PrograssFirst.add(one)
                val two = (java.lang.Double.valueOf(ChartDatePointsTwo[i]) / max * 100).toInt()
                PrograssSecond.add(two)
                val three = (java.lang.Double.valueOf(ChartDatePointsThree[i]) / max * 100).toInt()
                PrograssThird.add(three)
            }
        }

        prograss_first_one.progress = PrograssFirst[0]
        prograss_first_two.progress = PrograssFirst[1]
        prograss_first_three.progress = PrograssFirst[2]
        prograss_first_four.progress = PrograssFirst[3]
        prograss_first_five.progress = PrograssFirst[4]
        prograss_first_six.progress = PrograssFirst[5]
        prograss_first_seven.progress = PrograssFirst[6]


        prograss_second_one.progress = PrograssSecond[0]
        prograss_second_two.progress = PrograssSecond[1]
        prograss_second_three.progress = PrograssSecond[2]
        prograss_second_four.progress = PrograssSecond[3]
        prograss_second_five.progress = PrograssSecond[4]
        prograss_second_six.progress = PrograssSecond[5]
        prograss_second_seven.progress = PrograssSecond[6]

        prograss_third_one.progress = PrograssThird[0]
        prograss_third_two.progress = PrograssThird[1]
        prograss_third_three.progress = PrograssThird[2]
        prograss_third_four.progress = PrograssThird[3]
        prograss_third_five.progress = PrograssThird[4]
        prograss_third_six.progress = PrograssThird[5]
        prograss_three_seven.progress = PrograssThird[6]

        tv_x_one.text = (ChartXs[0])
        tv_x_two.text = (ChartXs[1])
        tv_x_three.text = (ChartXs[2])
        tv_x_four.text = (ChartXs[3])
        tv_x_five.text = (ChartXs[4])
        tv_x_six.text = (ChartXs[5])
        tv_x_seven.text = (ChartXs[6])


        tv_y_one.text = ChartYs[0]
        tv_y_two.text = ChartYs[1]
        tv_y_three.text = ChartYs[2]
        tv_y_four.text = ChartYs[3]
        tv_y_five.text = ChartYs[4]
        tv_y_six.text = ChartYs[5]
    }
}
